import numpy as np

from n_tuple.pattern import pattern_index


board_indexes = []
board_indexes.append(np.array(pattern_index([0, 1, 2, 3, 4, 5])))
board_indexes.append(np.array(pattern_index([4, 5, 6, 7, 8, 9])))
board_indexes.append(np.array(pattern_index([0, 1, 2, 4, 5, 6])))
board_indexes.append(np.array(pattern_index([4, 5, 6, 8, 9, 10])))

tables = []
tables.append(np.zeros((16, 16, 16, 16, 16, 16)))
tables.append(np.zeros((16, 16, 16, 16, 16, 16)))
tables.append(np.zeros((16, 16, 16, 16, 16, 16)))
tables.append(np.zeros((16, 16, 16, 16, 16, 16)))



class Net():
    def __init__(self, board) -> None:
        self.board = np.array(board)

    def estimate(self):
        self.match_index = []

        value = 0
        for index, table in zip(board_indexes, tables):
            self.match_index.append(self.board[index].tolist())
            for a,b,c,d,e,f in self.match_index[-1]:
                value += table[a,b,c,d,e,f]
        return value
    
    def update(self, u):
        u /= len(tables)*8
        value = 0
        for i, indexes in enumerate(self.match_index):
            for a,b,c,d,e,f in indexes:
                temp = tables[i][a,b,c,d,e,f] 
                temp += u

                tables[i][a,b,c,d,e,f] = temp
                value += temp
        return value


if __name__ == '__main__':
    board = np.arange(16)
    net = Net(board)

    print(net.estimate())
    print(net.update(0.001))
